/*
 * Copyright 2004 original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.jbfilter.impl;

import java.util.EnumMap;

import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Restrictions;
import org.jbfilter.core.JbfilterComparatorKeyword;

/**
 * Wraps the {@link JbfilterComparatorKeyword}'s with additional features for Hibernate filtering.
 * @author Marcus Adrian
 */
enum _JbfilterComparatorKeywordHibernate {
	eq(JbfilterComparatorKeyword.eq) {
		@Override
		public Criterion findCriterion(String propertyPath, Object value) {
			return Restrictions.eq(propertyPath, value);
		}
	},
	lt(JbfilterComparatorKeyword.lt) {
		@Override
		public Criterion findCriterion(String propertyPath, Object value) {
			return Restrictions.lt(propertyPath, value);
		}
	},
	gt(JbfilterComparatorKeyword.gt) {
		@Override
		public Criterion findCriterion(String propertyPath, Object value) {
			return Restrictions.gt(propertyPath, value);
		}
	},
	le(JbfilterComparatorKeyword.le) {
		@Override
		public Criterion findCriterion(String propertyPath, Object value) {
			return Restrictions.le(propertyPath, value);
		}
	},
	ge(JbfilterComparatorKeyword.ge) {
		@Override
		public Criterion findCriterion(String propertyPath, Object value) {
			return Restrictions.ge(propertyPath, value);
		}
	};

	private JbfilterComparatorKeyword wrapped;
	
	private static EnumMap<JbfilterComparatorKeyword, _JbfilterComparatorKeywordHibernate> wrapperRegistry
		= new EnumMap<JbfilterComparatorKeyword, _JbfilterComparatorKeywordHibernate>
			(JbfilterComparatorKeyword.class);
	
	static {
		for (_JbfilterComparatorKeywordHibernate wrapper : _JbfilterComparatorKeywordHibernate.values()) {
			wrapperRegistry.put(wrapper.wrapped, wrapper);
		}
	}

	public static _JbfilterComparatorKeywordHibernate fromJbfilterComparatorKeyword(JbfilterComparatorKeyword wrapped) {
		return wrapperRegistry.get(wrapped);
	}
	// constructor
	private _JbfilterComparatorKeywordHibernate(JbfilterComparatorKeyword wrapped) {
		this.wrapped = wrapped;
	}
	
	abstract public Criterion findCriterion(String propertyPath, Object value);
}
